package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hlw.hr_man.service.AddDeptService;
import com.hlw.hr_man.service.FindDeptService;
import com.hlw.hr_man.service.FuncService;
import com.hlw.hr_man.serviceImpl.AddDeptServiceImpl;
import com.hlw.hr_man.serviceImpl.FindDeptServiceImpl;
import com.hlw.hr_man.serviceImpl.FuncServiceImpl;

import net.sf.json.JSONArray;

/**权限分配中的树形下拉框的内容获取
 * 
 * @author 李豪
 *
 */
public class CurrentFunccontroller extends HttpServlet {
	FuncService funcservice = new FuncServiceImpl();
	FindDeptService fds = new FindDeptServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf8");
		req.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		HttpSession session = req.getSession();
		String enumber = req.getParameter("e_number");
		StringBuffer role_name = new StringBuffer();
		if (req.getParameter("role_id") != null) {
			int role_id = Integer.parseInt(req.getParameter("role_id"));
			StringBuffer dept_name = new StringBuffer(fds.findDeptNameByEnumber(enumber));
			if (role_id == 2) {
				role_name = dept_name.append("经理");
				String s = funcservice.getCurrentEmployeeFuncsByname(role_name.toString());
				JSONArray currentfuncs = funcservice.getFuncs(-1, s);
				pw.write(currentfuncs.toString());
			} if(role_id==1){
				role_name = new StringBuffer("普通员工");
				String s = funcservice.getCurrentEmployeeFuncsByname(role_name.toString());
				JSONArray currentfuncs = funcservice.getFuncs(-1, s);
				pw.write(currentfuncs.toString());
			}
		} else {
			String s = funcservice.getCurrentEmployeeFuncs(enumber);
			JSONArray currentfuncs = funcservice.getFuncs(-1, s);
			JSONArray allfuncs = (JSONArray) session.getAttribute("currentfuncs");
			pw.write(currentfuncs.toString());

		}
	}
}
